package sc.common.test.hopper.handler.server;

import java.util.concurrent.atomic.AtomicInteger;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.DefaultChannelGroup;

import sc.common.test.TestUtil;
import sc.common.test.hopper.enums.Verb;
import sc.common.test.hopper.handler.MyHandler;
import sc.common.test.hopper.model.MyMessage;

public class MyServerHandler extends MyHandler{

	public MyServerHandler(TestUtil tu) {
		super(tu);
	}
	
	static AtomicInteger COUNTER = new AtomicInteger(0);
	
	ChannelGroup clients = new DefaultChannelGroup("clients");
	
	@Override
	public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {
		super.channelOpen(ctx, e);
		clients.add(e.getChannel());
		MyMessage message = MyMessage.new4send();
		message.setVerb(Verb.Great);
		e.getChannel().write(message);
	}
	
	@Override
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
			throws Exception {
		TU.p("Message Received, message[%s]", e.getMessage());
		e.getChannel().write(e.getMessage());
	}

	public ChannelGroup getClients() {
		return clients;
	}
	
}